package com.example.a10928.weifriend.ui.personal_center

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.AdertAdapter
import com.example.a10928.weifriend.adapter.MyTeamAdapter
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.http.api.base.MyAdertBean
import com.example.a10928.weifriend.ui.BaseFragment
import com.example.a10928.weifriend.ui.task_center.TaskDetailsActivity
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_my_adert.*
import java.util.ArrayList

class AdertFragment : BaseFragment(){



    var datas = ArrayList<MyAdertBean.RecordsBean>()
    var adapter :AdertAdapter?=null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_my_adert, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        adapter = AdertAdapter(R.layout.item_my_adert, datas)
        recyc.setLayoutManager(LinearLayoutManager(activity))
        recyc.setAdapter(adapter)

        adapter?.setOnItemClickListener { adapter, view, position ->

            val intent = Intent(App.activity, TaskDetailsActivity::class.java)
            intent.putExtra(TaskDetailsActivity.SOURCE_TASKDETAILS_TYPE, TaskDetailsActivity.SOURCE_TAKE_TEH_TASK)
            intent.putExtra(TaskDetailsActivity.DATA_TAKE_THE_TASK, datas.get(position).ad_id)
            activity?.startActivity(intent)
        }
        var type=arguments?.getString(VariableName.TYPE)
        App.app().bpService.getMyAdvertisement(App.app().user.userId,type)
                .compose(RxUtil.io2main())
                .subscribe(
                        Consumer {
                            datas.clear()
                            datas.addAll(it.data.records)
                            adapter?.notifyDataSetChanged()
                        },
                        Consumer {
                            RxUtil.errorException(it)
                        }
                )

    }


}
